﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MINERP.DAL;
using MINERP.DAL.InfoConfig;
using MINERP.MODEL.InfoConfig;

namespace MINERP.BLL.InfoConfig
{
    public class PrinterBLL
    {
        #region //Private Fields


        #endregion


        #region //Public Properties

        #endregion



        #region //Public Methods
        /// <summary>
        /// Get printer list
        /// </summary>
        /// <returns></returns>
        public List<PrinterInfo> GetPrinterList()
        {
            List<PrinterInfo> list = new List<PrinterInfo>();
            List<printer> listPrinter;
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                listPrinter = entities.printer.ToList();

                foreach (var obj in listPrinter)
                {
                    //if (!obj.PrinterSharpening.IsLoaded)
                    //{
                    //    obj.PrinterSharpening.Load();
                    //}
                    if (!obj.GridSubAreaReference.IsLoaded)
                    {
                        obj.GridSubAreaReference.Load();
                    }
                    PrinterInfo printer = new PrinterInfo();
                    //if (obj.PrinterSharpening.Count>0)
                    //{
                    //    if (!obj.PrinterSharpening.First().paperFaceReference.IsLoaded)
                    //    {
                    //        obj.PrinterSharpening.First().paperFaceReference.Load();
                    //    }
                    //    if (!obj.PrinterSharpening.First().paperTypeReference.IsLoaded)
                    //    {
                    //        obj.PrinterSharpening.First().paperTypeReference.Load();
                    //    }
                    //    printer.PaperFaceName = obj.PrinterSharpening.First().paperFace.Name;
                    //    printer.PaperTypeName = obj.PrinterSharpening.First().paperType.Name;
                    //    printer.SharpVale = obj.PrinterSharpening.First().Sharpening;

                    //}
                    printer.PrinterID = obj.PrinterID;
                    printer.GridSubAreaName = obj.GridSubArea.SubAreaName;
                    printer.hotDirPath = obj.hotDirPath;
                    printer.ICCPath = obj.ICCPath;
                    printer.IsTransverse = (bool)obj.IsTransverse;
                    printer.MonitorDirPath = obj.MonitorDirPath;
                    printer.PDes = obj.PDes;
                    printer.PDPI = obj.PDPI;
                    printer.PLength = obj.PLength;
                    printer.PName = obj.PName;
                    printer.PType = obj.PType;
                    printer.PWidth = obj.PWidth;
                    printer.speed = obj.speed == null ? 0 : (decimal)obj.speed;
                    printer.status = obj.status;
                    printer.status2 = (int)obj.status2;
                    printer.status3 = obj.status3;
                    printer.MinHeight = (decimal)obj.MinHeight;
                    printer.MinWidth = (decimal) obj.MinWidth;
                    printer.MaxHeight = (decimal) obj.MaxHeight;
                    printer.MaxWidth = (decimal) obj.MaxWidth;
                    printer.IsMiddlePrint = obj.IsMiddlePrint;
                    printer.IsAutoCut =(bool) obj.IsAutoCut;
                    printer.CutFlagFrontOrBehind = (int)obj.CutFlagFrontOrBehind;
                    list.Add(printer);
                }
            }
            return list;
        }

        /// <summary>
        /// Add printer
        /// </summary>
        /// <param name="obj"></param>
        public void AddPrinter(PrinterInfo obj,string paperFaceName,string paperTypeName,int sharpingValue)
        {
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                printer printer = new printer();
                printer.GridSubArea = entities.GridSubArea.FirstOrDefault(g=>g.SubAreaName ==obj.GridSubAreaName);
                printer.hotDirPath = obj.hotDirPath;
                printer.ICCPath = obj.ICCPath;
                printer.IsTransverse = obj.IsTransverse;
                printer.MonitorDirPath = obj.MonitorDirPath;
                printer.PDes = obj.PDes;
                printer.PDPI = obj.PDPI;
                printer.PLength = obj.PLength;
                printer.PName = obj.PName;
                printer.PType = obj.PType;
                printer.PWidth = obj.PWidth;
                printer.speed = obj.speed;
                printer.status = obj.status;
                printer.status2 = obj.status2;
                printer.status3 = obj.status3;
                printer.GridNum = obj.GridNum;
                printer.MinHeight = (double)obj.MinHeight;
                printer.MinWidth = (double) obj.MinWidth;
                printer.MaxHeight = (double) obj.MaxHeight;
                printer.MaxWidth = (double) obj.MaxWidth;
                printer.IsMiddlePrint = obj.IsMiddlePrint;
                printer.IsAutoCut = obj.IsAutoCut;
                printer.CutFlagFrontOrBehind = obj.CutFlagFrontOrBehind;
                entities.AddToprinter(printer);
                entities.SaveChanges();
               
            }
            //PrinterDAL.AddPrinter(obj);
        }

        /// <summary>
        /// Update printerr
        /// </summary>
        /// <param name="obj"></param>
        public void UpdatePrinter(PrinterInfo obj, string paperFaceName, string paperTypeName, int sharpingValue)
        {
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                printer printer = entities.printer.First(p => p.PrinterID == obj.PrinterID);
                printer.hotDirPath = obj.hotDirPath;
                printer.MonitorDirPath = obj.MonitorDirPath;
                printer.ICCPath = obj.ICCPath;
                printer.IsTransverse = obj.IsTransverse;
                printer.PDes = obj.PDes;
                printer.PDPI = obj.PDPI;
                printer.PLength = obj.PLength;
                printer.PName = obj.PName;
                printer.PType = obj.PType;
                printer.PWidth = obj.PWidth;
                printer.GridNum = obj.GridNum;
                printer.MinHeight = (double)obj.MinHeight;
                printer.MinWidth = (double)obj.MinWidth;
                printer.MaxHeight = (double)obj.MaxHeight;
                printer.MaxWidth = (double)obj.MaxWidth;
                printer.IsMiddlePrint = obj.IsMiddlePrint;
                printer.IsAutoCut = obj.IsAutoCut;
                printer.GridSubArea = entities.GridSubArea.FirstOrDefault(g=>g.SubAreaName ==obj.GridSubAreaName);
                printer.CutFlagFrontOrBehind = obj.CutFlagFrontOrBehind;
                entities.SaveChanges();
            }
            //PrinterDAL.UpdatePrinter(obj);
        }

        /// <summary>
        /// Delete printer
        /// </summary>
        /// <param name="obj"></param>
        public void DeletePrinter(PrinterInfo obj)
        {
            using(newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                printer printer =entities.printer.FirstOrDefault(p=>p.PrinterID ==obj.PrinterID);
                entities.DeleteObject(printer);
                entities.SaveChanges();
            }
        }

        public printer GetPrinterObj(string name)
        {
            return PrinterDAL.GetPrinterObj(name);

        }
        #endregion




        public void UpdatePaperTypeICC(int printerId, string paperTypename, string iccPath, int sharpValue)
        {
            PrinterDAL.UpdatePapeTypeICC(printerId, paperTypename, iccPath, sharpValue);
        }

        public PrinterInfo  GetIccPath(int printerid, string papertypeName)
        {
            return PrinterDAL.GetIccPath(printerid, papertypeName);
        }
    }
}
